<template>
  <div>
    <van-nav-bar left-arrow @click-left="back"></van-nav-bar>
    <div class="login-form">
      <h3>短信登录</h3>
      <van-form ref="form">
        <van-field
          name="mobile"
          v-model="form.mobile"
          :rules="rules.mobile"
          placeholder="请输入手机号"
        ></van-field>
        <van-field
          v-model="form.code"
          :rules="rules.code"
          placeholder="请输入验证码"
        >
          <template #button>
            <!-- 冷却功能 -->
            <div v-if="totalTime === 6" @click="getCode" class="info">
              获取验证码
            </div>
            <div v-else class="info">{{ totalTime }}秒后重试</div>
          </template>
        </van-field>
        <van-button @click="submit" class="btn">登录</van-button>
      </van-form>
    </div>
  </div>
</template>
<script>
import { authorizations, smsCodes } from '@/api/user.js'
export default {
  name: '',
  data () {
    return {
      inteval: '',
      totalTime: 6,
      form: {
        mobile: '13200000000',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'onChange' },
          {
            validator: value => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              return reg.test(value)
            },
            message: '请输入正确手机号',
            trigger: 'onChange'
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'onChange' },
          {
            validator: value => value.length === 6,
            message: '请输入6位验证码',
            trigger: 'onChange'
          }
        ]
      }
    }
  },
  methods: {
    // 返回首页
    back () {
      this.$router.push('/')
    },
    // 登录
    submit () {
      this.$refs.form.validate().then(async () => {
        // 先loading,在请求数据成功后清除
        this.$toast.loading({ duration: 0 })
        const res = await authorizations(this.form)
        // 保存token到vuex
        this.$store.commit('user/setToken', res.data.token)
        this.$store.commit('user/setRefreshToken', res.data.refresh_token)

        // 登录成功后回到想要去的页面
        const redirect = this.$route.query.redirect || '/'
        this.$router.push(redirect)
        this.$toast.success('登录成功')
      })
      // .catch(() => {
      //   this.$toast.fail('验证失败')
      // })
    },

    // 获取验证码
    getCode () {
      this.$refs.form.validate('mobile').then(async () => {
        // 先禁止点击再开始计时
        this.totalTime--
        this.inteval = setInterval(() => {
          this.totalTime--
          if (this.totalTime <= 0) {
            clearInterval(this.inteval)
            this.totalTime = 6
          }
        }, 1000)
        await smsCodes(this.form.mobile)
      })
    }
  },
  beforeDestroy () {
    // 跳转到其他页面时清空计时器
    clearInterval(this.inteval)
  }
}
</script>
<style scoped lang="less">
.login-form {
  padding: 0 40px;
  h3 {
    margin: 40px 0;
    font-size: 30px;
    font-weight: 400;
  }
  .van-field {
    height: 60px;
    .info {
      color: #999;
    }
  }
  .btn {
    width: 100%;
    margin-top: 20px;
    background-color: #fd5347;
    color: #fff;
  }
}
::v-deep .van-cell {
  padding-left: 0;
  padding-right: 0;
}
::v-deep .van-nav-bar .van-icon {
  color: #999;
}
</style>
